//
//  MainTabBar.swift
//  demo01新浪微博
//
//  Created by 杨兵 on 15/5/10.
//  Copyright (c) 2015年 bing. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    lazy var composeButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        self.addSubview(btn)
        return btn
        }()

    // 设置item布局,将设置item布局
    private let countItem = 5
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 计算大小
        let itemW = UIScreen.mainScreen().bounds.width / CGFloat(countItem)
        let itemH = frame.size.height
        var index = 0
        
        for btnview in subviews as! [UIView] {
            
            if (btnview is UIControl) && !(btnview is UIButton) {
                
                // 计算位置
                var itemX = CGFloat(index) * itemW
                btnview.frame = CGRectMake(itemX, 0, itemW, itemH)
                
                // 判断是否是第三个,是第三个就向后挪动以为,为btn留下位置
                index += index == 1 ? 2 : 1
            }
        }
        // 将btn添加到视图中
        composeButton.bounds = CGRectMake(0, 0, itemW, itemH)
        composeButton.center = CGPointMake(self.bounds.width/2, itemH/2)
        
        
    }
    
}
